﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

public partial class Business_projecthistory : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindddl();
            bind(true);
        }
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        bind(true);
    }

    protected void bindddl()
    {
        //绑定学院的下拉框
        PM.Code.Web.controlbind.BindDropDownList(this.ddlType, "select id,btnname from Sys_Guide", "btnname", "id");
        this.ddlType.Items.Insert(0, new ListItem("全部", "100"));
        this.ddlType.SelectedIndex = 0;
    }
    //行编辑
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        this.GridView1.EditIndex = e.NewEditIndex;
        bind(false);
        //注意：关于gridview的编辑的次序，当点击编辑按钮时，先一般的是你获取了行的索引号，再进行重新绑定，
        //再执行绑定动作，但是绑定（绑定事件也触发）时，它会按照要编辑的索引号对需要编辑的那一行的单元格
        //进行“特殊处理”，先销毁Label，再创建TextBox，所以你要寻找TextBox的话你必须要等它绑定完成后再去那一行中找控件
        //否则的话，如果在绑定前对寻找控件，
        //因为根本还没有创建TextBox控件，它会警告说“index超出索引”。而这也解释了下面的RowDataBound事件，
        //为什么要加if (e.Row.Cells[6].Text.Trim() != string.Empty && e.Row.Cells[7].Text.Trim() != string.Empty)座位判断，
        //难道绑定的时候没有载入数据吗？当然不是，它一开始是创建了，但是之后又销毁了，所以留下的
        //仅仅是TextBox控件，你直接这样e.Row.Cells[7].Text.Trim()去做判断它是显示为空的。 Good luck!
        TextBox txt1 = (TextBox)(this.GridView1.Rows[e.NewEditIndex].Cells[7].Controls[0]);
        TextBox txt2 = (TextBox)(this.GridView1.Rows[e.NewEditIndex].Cells[8].Controls[0]);
        txt1.Attributes.Add("id", "txtOpenDate");
        txt2.Attributes.Add("id", "txtCloseDate");
        txt1.Attributes.Add("onfocus", "WdatePicker({autoPickDate:true,startDate:'%y-%M-01 00:00:00',dateFmt:'yyyy/MM/dd HH:mm:ss',alwaysUseStartDate:true,isShowClear:false,readOnly:true,maxDate:'#F{$dp.$D(\\'txtCloseDate\\',{d:-1})||\\'2020-10-01\\'}'})");
        txt2.Attributes.Add("onfocus", "WdatePicker({autoPickDate:true,startDate:'%y-%M-01 00:00:00',dateFmt:'yyyy/MM/dd HH:mm:ss',alwaysUseStartDate:true,isShowClear:false,readOnly:true,minDate:'#F{$dp.$D(\\'txtOpenDate\\',{d:+1})}',maxDate:'2020-10-01'})");
    }
    //取消行编辑
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        this.GridView1.EditIndex = -1;
        bind(false);
    }
    //每行绑定后触发
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.Cells[7].Text.Trim() != string.Empty && e.Row.Cells[8].Text.Trim() != string.Empty && e.Row.Cells[5].Text.Trim() != string.Empty)
            {
                string opentime = e.Row.Cells[7].Text.Trim();
                string closetime = e.Row.Cells[8].Text.Trim();
                string pstatus = e.Row.Cells[5].Text.Trim();
                DateTime nowtime = DateTime.Now;
                //因为RowDataBound是发生在数据绑定之后，所以我们可以
                //判断时间来确定其状态
                if (DateTime.Parse(opentime) > nowtime)
                {
                    e.Row.Cells[6].Text = "未执行";
                }
                else if (DateTime.Parse(closetime) < nowtime)
                {
                    e.Row.Cells[6].Text = "已过期";
                    Image img = (Image)(e.Row.Cells[9].Controls[0]);
                    img.ImageUrl = "~/js/themes/icons/disable_pencil.png";
                    e.Row.Cells[9].Enabled = false;
                }
                else if (pstatus == "已完成")
                {
                    e.Row.Cells[6].Text = "正在执行";
                    ImageButton ib = (ImageButton)e.Row.FindControl("imgbtndel");
                    ib.ImageUrl = "~/js/themes/icons/undo.png";
                    ib.ToolTip = "强制中断！";
                    ib.OnClientClick = "return confirm(\"强行中断后教职工不能申报，您确定要中断此计划？\");";
                    Image img = (Image)(e.Row.Cells[9].Controls[0]);
                    img.ImageUrl = "~/js/themes/icons/disable_pencil.png";
                    e.Row.Cells[9].Enabled = false;
                }
                else
                    e.Row.Cells[6].Text = "无法执行";
            }
            else
                e.Row.Cells[6].Text = "修改中";
        }
    }
    //行更新
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        PM.PMBusiness.projectschedual pro = new PM.PMBusiness.projectschedual();
        DateTime adate = DateTime.Now;
        //获取行主键值和类型值
        int id = Convert.ToInt16(GridView1.DataKeys[e.RowIndex].Value);
        string type = GridView1.Rows[e.RowIndex].Cells[2].Text.Trim();
        //获取修改值
        string opendate = ((TextBox)GridView1.Rows[e.RowIndex].Cells[7].Controls[0]).Text.Trim();
        string closedate = ((TextBox)GridView1.Rows[e.RowIndex].Cells[8].Controls[0]).Text.Trim();
        if (opendate == string.Empty || closedate == string.Empty)
        {
            PM.Code.Web.alertmessage.ShowMsg(this.UpdatePanel1, "时间不能为空！");
            return;
        }
        if(DateTime.Parse(opendate.Trim())<adate)
        {
            PM.Code.Web.alertmessage.ShowMsg(this.UpdatePanel1, "开启时间不能小于现在时间！");
            return;
        }
        //检测数据库所输入的时间是否合法
        string result = pro.PlanCheck(id,type, DateTime.Parse(opendate), DateTime.Parse(closedate));
        if (result.Trim()=="0002")
        {
            if (pro.Update_ProjectPlan(id, opendate, closedate) != 0)
            {
                PM.Code.Web.alertmessage.ShowMsg(this.UpdatePanel1, "更新成功！");
                this.GridView1.EditIndex = -1;
            }
        }
        else
        {
            PM.Code.Web.alertmessage.ShowMsg(this.UpdatePanel1, "时间和"+result+"有冲突！不能修改");
            this.GridView1.EditIndex = -1;
        }
        bind(false);
    }
    //行内触发命令
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        //实例化计划类
        PM.PMBusiness.projectschedual pro = new PM.PMBusiness.projectschedual();
        int index=Convert.ToInt32(e.CommandArgument);
        int id = Convert.ToInt32(GridView1.DataKeys[index].Value);
        string statustext = this.GridView1.Rows[index].Cells[6].Text ;
        if (e.CommandName == "deleteplan")
        {
            if (statustext == "正在执行")
            {
                if(pro.Update_PlanStatus(id,"中断")>0)
                    Response.Redirect("projectpaused.aspx?planid=" + id.ToString() + "&new=1");
            }
            else
            {
                if (pro.Delete_ProjectPlan(id) > 0)
                    PM.Code.Web.alertmessage.ShowMsg(this.UpdatePanel1, "已删除此项目计划！");
                bind(false);
            }   
        }
    }
    //搜索提交
    protected void btnSearch_Click(object sender, ImageClickEventArgs e)
    {
        this.AspNetPager1.CurrentPageIndex = 0;
        bind(true);
    }
    protected void bind(bool getCount)
    {
        try
        {
            StringBuilder sqlstr = new StringBuilder(" 1=1 ");
            getWhere(ref sqlstr);
            PM.Code.Web.controlbind.BindGridViewPublicProc(GridView1, AspNetPager1, "View_ProjectPlan", "id", "*", "id", sqlstr.ToString(), true);
        }
        catch (System.Exception ex)
        {
            throw ex;
        }
    }
    /// <summary>
    /// 生成sql语句
    /// </summary>
    /// <param name="sqlstrwhere">sql语句</param>
    public void getWhere(ref StringBuilder sqlstr)
    {
        if (this.txtPlanCode.Text.Trim() != string.Empty)
            sqlstr.Append(" and plancode='").Append(this.txtPlanCode.Text.Trim()).Append("'");
        else
        {
            if (this.ddlType.SelectedIndex != 0)
                sqlstr.Append(" and type='").Append(this.ddlType.SelectedValue.Trim()).Append("'");
            if (this.ddlLevel.SelectedIndex != 0)
                sqlstr.Append(" and plevel='").Append(this.ddlLevel.SelectedValue.Trim()).Append("'");
            if (this.ddlPType.SelectedIndex != 0)
                sqlstr.Append(" and ptype='").Append(this.ddlPType.SelectedValue.Trim()).Append("'");
            if (this.ddlStatus1.SelectedIndex != 0)
                sqlstr.Append(" and pstatus='").Append(this.ddlStatus1.SelectedValue.Trim()).Append("'");
            if (this.txtTime1.Value.Trim() != string.Empty && this.txtTime2.Value.Trim() != string.Empty)
                sqlstr.Append(" and builtdate>='").Append(this.txtTime1.Value.Trim()).Append("' and builtdate<='").Append(this.txtTime2.Value.Trim()).Append("' ");
            if(this.ddlStatus.SelectedIndex!=0)
            {
                DateTime nowtime = DateTime.Now;
                if(this.ddlStatus.SelectedValue.Equals("2"))
                    sqlstr.Append(" and closedate<'").Append(nowtime.ToString()).Append("'");
                else if(this.ddlStatus.SelectedValue.Equals("3"))       
                    sqlstr.Append(" and opendate<='").Append(nowtime.ToString()).Append("' and closedate>='").Append(nowtime.ToString()).Append("'");
                else if(this.ddlStatus.SelectedValue.Equals("4"))       
                    sqlstr.Append(" and opendate>'").Append(nowtime.ToString()).Append("'");
            }
        }
    }
}
